package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.jhgl.HkjhDao;
import com.hnkywl.sts.dao.report.YqyskDao;
import com.hnkywl.sts.entity.jhgl.Hkjh;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-2
 * Time: 上午10:52
 */
@Service
public class HkjhService {
    private HkjhDao hkjhDao;
    private BmService bmService;
    private YqyskDao yqyskDao;

    public String findHkjhList(Long gsid, Long bmid, Integer year, Integer month){
        Date startDate = null;
        Date endDate = null;
        if (year != null && month != null) {
            startDate = DateUtils.truncate(DateUtils.setMonths(DateUtils.setYears(new Date(), year), month - 1), 2);
            endDate = DateUtils.truncate(DateUtils.setMonths(DateUtils.setYears(new Date(), year), month), 2);
        }
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Map<String, List> map = hkjhDao.hkjhList(gsid, bmid, startDate, endDate);
        List<Map> grid = map.get("rows") == null ? new ArrayList<Map>() : map.get("rows");
        List<Map> footer = map.get("footer") == null ? new ArrayList<Map>() : map.get("footer");
        return "{\"rows\":"+gson.toJson(grid)+",\"footer\":"+gson.toJson(footer)+"}";
    }

    @Transactional
    public void save(Hkjh hkjh){
        if(hkjhDao.findByGsAndNfAndYf(hkjh.getGsid(),hkjh.getNf(),hkjh.getYf()) != null){
            throw new RuntimeException("本月已经添加回款计划");
        }
        hkjh.setCreateUser(YgService.getCurrentYg());
        hkjh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        hkjh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        hkjh.setDateCreated(new Date());
        hkjh.setLastUpdated(hkjh.getDateCreated());
        hkjh.setJhdh(bmService.getHkjhbm());
        for(int i=0;i<hkjh.getHkjhmxList().size();i++){
            hkjh.getHkjhmxList().get(i).setXj(hkjh.getHkjhmxList().get(i).getXh()+hkjh.getHkjhmxList().get(i).getCd());
        }
        hkjhDao.save(hkjh);
    }

    public String findHkjh(Long gsId,Long bmId,Integer year,Integer month){
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            dateStart = DateUtils.truncate(DateUtils.setMonths(DateUtils.setYears(new Date(), year), month - 1), 2);
            dateEnd = DateUtils.truncate(DateUtils.setMonths(DateUtils.setYears(new Date(), year), month), 2);
        }
        Map<String, List> result = yqyskDao.ysktjList(0,gsId,bmId,dateStart,dateEnd);
        List<Map> rows = result.get("rows");
        List<Map> footer = result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + "}";
        return json;
    }

//    @Transactional
//    public void createHkjhByZddhht(Xsht xsht) {
//        Hkjh hkjh = new Hkjh();
//        Calendar c = Calendar.getInstance();
//        c.setTime(xsht.getQdrq());
//        c.add(Calendar.DATE, xsht.getHkqx());
//        hkjh.setHksj(c.getTime());
//        if ("zddh".equalsIgnoreCase(xsht.getXslx())) {
//            if ("010".equalsIgnoreCase(xsht.getJsyj().getBm()))
//                hkjh.setJsfs("fhsj");
//            if ("011".equalsIgnoreCase(xsht.getJsyj().getBm()))
//                hkjh.setJsfs("yfksj");
//        }
//        hkjh.setType(xsht.getXslx());
//        hkjh.setXsgs(xsht.getWlfgs());
//        hkjh.setKh(xsht.getHzf());
//        if (xsht.getHtmxList() != null) {
//            for (Htmx htmx : xsht.getHtmxList()) {
//                hkjh.setJhhkje(hkjh.getJhhkje() + htmx.getBhsj());
//            }
//        }
//        hkjh.setXsht(xsht);
//        hkjh.setCreateUser(YgService.getCurrentYg());
//        hkjh.setDateCreated(new Date());
//        hkjhDao.save(hkjh);
//    }
//
//    @Transactional
//    public void createHkjhByXsd(Xsd xsd) {
//        Hkjh hkjh = new Hkjh();
//        Calendar c = Calendar.getInstance();
//        c.setTime(xsd.getFhsj());
//        Xsht xsht = xsd.getXsht();
//        if (xsht != null) {
//            //合同销售
//            c.add(Calendar.DATE, xsht.getHkqx());
//            hkjh.setType("htxs");
//        } else {
//            //库存零售
//            hkjh.setType("kcls");
//        }
//        hkjh.setHksj(c.getTime());
//        hkjh.setJsfs("fhsj");
//        hkjh.setXsgs(xsd.getXsgs());
//        hkjh.setKh(xsd.getKh());
//        if (xsd.getXsmxList() != null) {
//            for (Xsmx xsmx : xsd.getXsmxList()) {
//                hkjh.setJhhkje(hkjh.getJhhkje() + xsmx.getBhsj());
//            }
//        }
//        hkjh.setXsd(xsd);
//        hkjh.setCreateUser(YgService.getCurrentYg());
//        hkjh.setDateCreated(new Date());
//        hkjhDao.save(hkjh);
//    }
//
//    public String searchHkjh(Integer year, Integer month, Long xsgsId, Long xsbmId) {
//        Map<String, List> result = hkjhDao.searchHkjh(year, month, xsgsId, xsbmId);
//        Gson gson = new Gson();
//        String row = gson.toJson(result.get("detail"));
//        String footer = gson.toJson(result.get("total"));
//        String json = "{\"rows\":" + row + ",\"footer\":" + footer + "}";
//        return json;
//    }

    @Autowired
    public void setHkjhDao(HkjhDao hkjhDao) {
        this.hkjhDao = hkjhDao;
    }
    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
    @Autowired
    public void setYqyskDao(YqyskDao yqyskDao) {
        this.yqyskDao = yqyskDao;
    }
}
